package com.octopus.optiworks.controller;

import java.io.File;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.octopus.optiworks.action.StepActionUtil;
import com.octopus.optiworks.model.*;
import com.octopus.optiworks.service.StepService;
import com.octopus.optiworks.service.UserService;
import com.octopus.optiworks.service.UtilService;
import com.octopus.optiworks.tool.CreateDataSheet0386RevA;

@Controller
@RequestMapping("/step")
public class StepController {
	
	private static Log log = LogFactory.getLog(StepController.class);
	
	private InitProcessValidator initProcessValidator = new InitProcessValidator();
	
	private StepValidator stepValidator = new StepValidator();
	
	@Autowired
	private StepService stepService;
	
	@Autowired
	private UtilService utilService;
	
	@Autowired
    private UserService userService;
	
	@RequestMapping(value = "index.o", method= RequestMethod.GET)
	public String taskIndex(Model model, HttpServletRequest request, @ModelAttribute SearchCommand command) throws Exception{
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
    	command.clear();
    	String step = request.getParameter("step");
    	String archive = request.getParameter("archive");
    	if(step != null)
    		command.setStep(step);
    	if(archive != null)
    		command.setArchive(true);
    	request.getSession().setAttribute("searchCommandIndex", command);
    	String unlock = request.getParameter("unlock");
    	if(unlock != null){
    		this.stepService.unlockProduct(new Long(unlock));
    	}
		return this.goIndex(model, request, command, true);
	}
	
	@RequestMapping(value = "index.o", method= RequestMethod.POST)
	public String taskSearch(Model model, HttpServletRequest request, @ModelAttribute SearchCommand command) throws Exception{
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
    	command.setArchive(((SearchCommand)request.getSession().getAttribute("searchCommandIndex")).getArchive());
    	request.getSession().setAttribute("searchCommandIndex", command);
		return this.goIndex(model, request, command, true);
	}
	
	@RequestMapping(value = "archive.o", method= RequestMethod.GET)
	public String archive(Model model, HttpServletRequest request) throws Exception{
		SearchCommand command = (SearchCommand)request.getSession().getAttribute("searchCommandIndex");
		this.stepService.archiveProducts(command);
		return this.goIndex(model, request, command, true);
	}
	
	@RequestMapping(value = "unarchive.o", method= RequestMethod.GET)
	public String unarchive(Model model, HttpServletRequest request) throws Exception{
		SearchCommand command = (SearchCommand)request.getSession().getAttribute("searchCommandIndex");
		this.stepService.unarchiveProducts(command);
		return this.goIndex(model, request, command, true);
	}
	
	@RequestMapping(value = "query.o", method= RequestMethod.GET)
	public String queryIndex(Model model, HttpServletRequest request, @ModelAttribute SearchCommand command) throws Exception{
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
    	command.clear();
    	String archive = request.getParameter("archive");
    	if(archive != null)
    		command.setArchive(true);
    	request.getSession().setAttribute("searchCommandQuery", command);
		return this.goIndex(model, request, command, false);
	}
	
	@RequestMapping(value = "query.o", method= RequestMethod.POST)
	public String querySearch(Model model, HttpServletRequest request, @ModelAttribute SearchCommand command) throws Exception{
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
    	command.setArchive(((SearchCommand)request.getSession().getAttribute("searchCommandQuery")).getArchive());
    	request.getSession().setAttribute("searchCommandQuery", command);
		return this.goIndex(model, request, command, false);
	}
	
	@RequestMapping(value = "indexpage.o", method= RequestMethod.GET)
	public String taskIndexPage(Model model, HttpServletRequest request, @ModelAttribute SearchCommand command) throws Exception{
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
    	Object sessionCommand = request.getSession().getAttribute("searchCommandIndex");
    	if(sessionCommand != null)
    		command.copy(((SearchCommand)sessionCommand));
    	int offset = Integer.parseInt(request.getParameter("pager.offset"));
    	command.setOffset(offset);
		return this.goIndex(model, request, command, true);
	}
	
	@RequestMapping(value = "querypage.o", method= RequestMethod.GET)
	public String taskQueryPage(Model model, HttpServletRequest request, @ModelAttribute SearchCommand command) throws Exception{
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
    	Object sessionCommand = request.getSession().getAttribute("searchCommandQuery");
    	if(sessionCommand != null)
    		command.copy(((SearchCommand)sessionCommand));
    	int offset = Integer.parseInt(request.getParameter("pager.offset"));
    	command.setOffset(offset);
		return this.goIndex(model, request, command, false);
	}
	
	@RequestMapping(value = "start.o", method= RequestMethod.GET)
	public String initProcessForm(Model model, @ModelAttribute InitProcessCommand command) throws Exception{
    	model.addAttribute("templates", this.stepService.getAllTemplates());
    	List<StepSpec> specs = new ArrayList<StepSpec>();
    	StepSpec naSpec = new StepSpec();
    	naSpec.setId(new Long(-1));
    	naSpec.setName("N/A");
    	specs.add(naSpec);
    	specs.addAll(this.stepService.getAllStepSpecs());
    	model.addAttribute("specs", specs);
    	return "step/template";
	}
	
	@RequestMapping(value = "start.o", method = RequestMethod.POST)
	public String start(Model model, HttpServletRequest request, @ModelAttribute InitProcessCommand command, BindingResult errors) throws Exception{
		initProcessValidator.validate(command, errors);
        if( errors.hasErrors() ) {
        	model.addAttribute("templates", this.stepService.getAllTemplates());
            return initProcessForm(model, command);
        }
		this.stepService.newProduct(command);
		return "redirect:/step/index.o";
	}
	
	@RequestMapping(value = "goon.o", method = RequestMethod.GET)
	public String goon(Model model, @RequestParam("productId") Long productId, HttpServletRequest request, @ModelAttribute SearchCommand commands) throws Exception{
		Employee user =  this.userService.getCurrentUser();
		if(!this.stepService.lockProduct(productId, user.getUsername())){
			String lockError = this.stepService.getProductMeta(productId).getLockedBy();
			model.addAttribute("lockError",lockError);
			return this.goIndex(model, request, commands, true);
		}
		return this.goEdit(model, request, productId);
	}
	
	@RequestMapping(value = "sedit.o", method = RequestMethod.GET)
	public String sedit(Model model, @RequestParam("productId") Long productId, HttpServletRequest request, @ModelAttribute SearchCommand commands) throws Exception{
		Employee user =  this.userService.getCurrentUser();
		if(!this.stepService.lockProduct(productId, user.getUsername())){
			String lockError = this.stepService.getProductMeta(productId).getLockedBy();
			model.addAttribute("lockError",lockError);
			return this.goIndex(model, request, commands, true);
		}
		request.setAttribute("Edit","Edit");
		return this.goEdit(model, request, productId);
	}
	
	@RequestMapping(value = "load140.o", method = RequestMethod.POST)
	public String load140(Model model, HttpServletRequest request, @RequestParam("productId") Long productId) throws Exception{
		Object obj = this.submitData(request);
		this.stepService.loadStep140TestingData(productId, (Step140_1Data)obj);
		String edit = request.getParameter("Edit");
		if(edit != null && edit.length() > 0)
			request.setAttribute("Edit", "Edit");
		return this.goEditWithError(model, request, productId, obj);
	}
	
	@RequestMapping(value = "view.o", method = RequestMethod.GET)
	public String view(Model model, @RequestParam("productId") Long productId, HttpServletRequest request) throws Exception{
		return this.goView(model, request, productId);
	}
	
	@RequestMapping(value = "delete.o", method = RequestMethod.GET)
	public String delete(Model model, @RequestParam("productId") Long productId, HttpServletRequest request, @ModelAttribute SearchCommand command) throws Exception{
		this.stepService.deleteProduct(productId);
		command.setArchive(false);
		return this.goIndex(model, request, command, true);
	}
	
	@RequestMapping(value = "export.o", method = RequestMethod.GET)
	public void export(Model model, @RequestParam("productId") Long productId, HttpServletRequest request, HttpServletResponse response) throws Exception{
		String path = request.getSession().getServletContext().getRealPath("/WEB-INF/0386RevA.xls");
		File template=new File(path);
		OutputStream out=response.getOutputStream();
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition","attachment; filename=datasheet.xls");
		Product product = this.stepService.getProduct(productId);
		CreateDataSheet0386RevA.getInstance().exportExcel(product.getDataSheet(), template, out);
	}
	
	@RequestMapping(value = "lock.o", method= RequestMethod.GET)
	public String lock(Model model, @RequestParam("productId") Long productId, HttpServletRequest request, @ModelAttribute SearchCommand command) throws Exception{
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
    	Object sessionCommand = request.getSession().getAttribute("searchCommandIndex");
    	if(sessionCommand != null)
    		command.copy(((SearchCommand)sessionCommand));
    	if(request.getParameter("pager.offset") != null){
	    	int offset = Integer.parseInt(request.getParameter("pager.offset"));
	    	command.setOffset(offset);
    	}
    	this.stepService.lockProduct(productId, user.getUsername());
		return this.goIndex(model, request, command, true);
	}
	
	@RequestMapping(value = "unlock.o", method= RequestMethod.GET)
	public String unlock(Model model, @RequestParam("productId") Long productId, HttpServletRequest request, @ModelAttribute SearchCommand command) throws Exception{
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
    	Object sessionCommand = request.getSession().getAttribute("searchCommandIndex");
    	if(sessionCommand != null)
    		command.copy(((SearchCommand)sessionCommand));
    	if(request.getParameter("pager.offset") != null){
	    	int offset = Integer.parseInt(request.getParameter("pager.offset"));
	    	command.setOffset(offset);
    	}
    	this.stepService.unlockProduct(productId);
		return this.goIndex(model, request, command, true);
	}
	
	@RequestMapping(value = "save.o", method = RequestMethod.POST)
	public String save(Model model, HttpServletRequest request, @RequestParam("productId") Long productId) throws Exception{
		Object obj = this.submitData(request);
		BindingResult errors = new  BeanPropertyBindingResult(obj, "product");
		stepValidator.validate(obj, errors);
		String edit = request.getParameter("Edit");
		if(edit != null && edit.length() > 0)
			request.setAttribute("Edit", "Edit");
		if(errors.hasErrors()){
			model.addAttribute("errors", errors.getAllErrors());
			return this.goEditWithError(model, request, productId, obj);
		}
		this.stepService.saveData(obj, false, null);
		return this.goEdit(model, request, productId);
	}
	
	@RequestMapping(value = "submit.o", method = RequestMethod.POST)
	public String submit(Model model, HttpServletRequest request, @RequestParam("step") String step, @RequestParam("productId") Long productId, @ModelAttribute SearchCommand command) throws Exception{
		Object obj = this.submitData(request);
		BindingResult errors = new  BeanPropertyBindingResult(obj, "product");
		stepValidator.validate(obj, errors);
		if(errors.hasErrors()){
			model.addAttribute("errors", errors.getAllErrors());
			return this.goEditWithError(model, request, productId, obj);
		}
		Map<String, String> map = null;
		String option = request.getParameter(StepActionUtil.TESTRESULT);
		if(option != null){
			map = new HashMap<String, String>();
			map.put(StepActionUtil.TESTRESULT, option);
		}
		this.stepService.saveDataAndSignal(obj, productId, step, map);
		command.clear();
		this.stepService.unlockProduct(productId);
		return this.goIndex(model, request, command, true);
	}
	
	private Object submitData(HttpServletRequest request){
		Object obj = null;
		try {
			Class<?> clazz = Class.forName(request.getParameter("className"));
			obj = clazz.newInstance();
			Field[] fields = clazz.getDeclaredFields();
			if(fields != null && fields.length > 0){
				for(Field field : fields){
					String property = field.getName();
					String value = request.getParameter(property);
					if(value != null && value.equals("_other_"))
						value = request.getParameter(property + "_other");
					try{
						if(field.getType().getName().equals("java.lang.String")){
							PropertyUtils.setProperty(obj, property, value);
						}else if(field.getType().getName().equals("java.lang.Long")){
							PropertyUtils.setProperty(obj, property, new Long(value));
						}else if(field.getType().getName().equals("java.lang.Boolean")){
							if("true".equals(value))
								PropertyUtils.setProperty(obj, property, true);
							else if("false".equals(value))
								PropertyUtils.setProperty(obj, property, false);
						}else if(field.getType().getName().equals("java.util.Date")){
							SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", new Locale("en"));
							if(value != null && value.length() > 0)
								try {
									PropertyUtils.setProperty(obj, property, format.parse(value));
								} catch (ParseException e) {
									e.printStackTrace();
								}
						}else if(field.getType().getName().equals("com.octopus.optiworks.model.Product")){
							Product product = new Product();
							product.setId(new Long(value));
							PropertyUtils.setProperty(obj, property, product);
						}else if(field.getType().getName().equals("com.octopus.optiworks.model.Template")){
							Template template = new Template();
							template.setId(new Long(value));
							PropertyUtils.setProperty(obj, property, template);
						}
					}catch(NoSuchMethodException e){
						log.debug(e);
					}
				}
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return obj;
	}

	private String goIndex(Model model, HttpServletRequest request, SearchCommand command, boolean permission){
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
    	List<String> statuses = new ArrayList<String>();
    	statuses.add(StepActionUtil.STEP_ALL);
    	statuses.add(StepActionUtil.STEP_00);
    	statuses.add(StepActionUtil.STEP_10_1);
    	statuses.add(StepActionUtil.STEP_10_2);
    	statuses.add(StepActionUtil.STEP_20);
    	statuses.add(StepActionUtil.STEP_30_1);
    	statuses.add(StepActionUtil.STEP_30_2);
    	statuses.add(StepActionUtil.STEP_40);
    	statuses.add(StepActionUtil.STEP_50);
    	statuses.add(StepActionUtil.STEP_60);
    	statuses.add(StepActionUtil.STEP_70);
    	statuses.add(StepActionUtil.STEP_80);
    	statuses.add(StepActionUtil.STEP_90);
    	statuses.add(StepActionUtil.STEP_100_1);
    	statuses.add(StepActionUtil.STEP_100_2);
    	statuses.add(StepActionUtil.STEP_100_3);
    	statuses.add(StepActionUtil.STEP_110);
    	statuses.add(StepActionUtil.STEP_120_1);
    	statuses.add(StepActionUtil.STEP_120_2);
    	statuses.add(StepActionUtil.STEP_130);
    	statuses.add(StepActionUtil.STEP_140_1);
    	statuses.add(StepActionUtil.STEP_140_2);
    	statuses.add(StepActionUtil.STEP_150);
    	statuses.add(StepActionUtil.STEP_160);
    	statuses.add(StepActionUtil.STEP_170_1);
    	statuses.add(StepActionUtil.STEP_170_2);
    	statuses.add(StepActionUtil.STEP_180);
    	statuses.add(StepActionUtil.STEP_COMPLETE);
    	statuses.add(StepActionUtil.TESTRESULT_FAILURE);
    	statuses.add(StepActionUtil.TESTRESULT_WAIVE);
    	model.addAttribute("statuses", statuses);
    	List<Employee> users = new ArrayList<Employee>();
    	Employee all = new Employee();
    	all.setUsername("all");
    	all.setSerial("all");
    	users.add(all);
    	users.addAll(this.userService.getAllUsers());
    	model.addAttribute("users", users);
		List<Product> list;
		if(permission)
			list = this.stepService.getProductsByPermissions(command);
		else
			list = this.stepService.getProductsNoPermissions(command);
		request.setAttribute("products", list);
		request.setAttribute("searchCommand", command);
		if(command == null)
			command = new SearchCommand();
		log.debug("Total product number: " + command.getTotal());
		String page = permission?"step/index":"step/query";
		return page;
	}
	
	private String goEdit(Model model, HttpServletRequest request, Long productId){
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
    	model.addAttribute("users", this.userService.getAllUsers());
		Product product = this.stepService.getProduct(productId);
		log.debug("Process Instance: " + product.getProcessId());
		String step = request.getParameter("step");
		if(step == null || step.length() == 0){
			Set<String> actives = this.stepService.findActiveActivityNames(product.getProcessId());
			if(actives != null && actives.size() > 0){
				for(String active : actives){
					request.setAttribute(active, active);
					log.debug("Active activity: " + active);
				}
			}
		}else{
			request.setAttribute(step, step);
		}
		request.setAttribute("product", product);
		List<Ituch> ituchs = new ArrayList<Ituch>();
		ituchs.add(new Ituch());
		ituchs.addAll(this.utilService.getAllItuchs());
		List<Itunm> itunms = new ArrayList<Itunm>();
		itunms.add(new Itunm());
		itunms.addAll(this.utilService.getAllItunms());
		request.setAttribute("ituchs", ituchs);
		request.setAttribute("itunms", itunms);
		String[] failedCells = this.stepService.findFailedCells(product.getProcessId());
		request.setAttribute("failedCells", failedCells);
		String page = "step/edit";
		return page;
	}
	
	private String goEditWithError(Model model, HttpServletRequest request, Long productId, Object obj){
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
    	model.addAttribute("users", this.userService.getAllUsers());
		Product product = this.stepService.getProduct(productId);
		log.debug("Process Instance: " + product.getProcessId());
		if(obj instanceof Product){
			Product p = (Product)obj;
			p.setStep10_1Datas(product.getStep10_1Datas());
			p.setStep10_2Datas(product.getStep10_2Datas());
			p.setStep20Datas(product.getStep20Datas());
			p.setStep30_1Datas(product.getStep30_1Datas());
			p.setStep30_2Datas(product.getStep30_2Datas());
			p.setStep40Datas(product.getStep40Datas());
			p.setStep50Datas(product.getStep50Datas());
			p.setStep60Datas(product.getStep60Datas());
			p.setStep70Datas(product.getStep70Datas());
			p.setStep80Datas(product.getStep80Datas());
			p.setStep90Datas(product.getStep90Datas());
			p.setStep100_1Datas(product.getStep100_1Datas());
			p.setStep100_2Datas(product.getStep100_2Datas());
			p.setStep100_3Datas(product.getStep100_3Datas());
			p.setStep110Datas(product.getStep110Datas());
			p.setStep120_1Datas(product.getStep120_1Datas());
			p.setStep120_2Datas(product.getStep120_2Datas());
			p.setStep130Datas(product.getStep130Datas());
			p.setStep140_1Datas(product.getStep140_1Datas());
			p.setStep140_2Datas(product.getStep140_2Datas());
			p.setStep150Datas(product.getStep150Datas());
			p.setStep160Datas(product.getStep160Datas());
			p.setStep170_1Datas(product.getStep170_1Datas());
			p.setStep170_2Datas(product.getStep170_2Datas());
			p.setStep180Datas(product.getStep180Datas());
			p.setDataSheet(product.getDataSheet());
			product = p;
		}else if(obj instanceof Step10_1Data){
			product.getStep10_1Datas().clear();
			product.getStep10_1Datas().add((Step10_1Data)obj);
		}else if(obj instanceof Step10_2Data){
			product.getStep10_2Datas().clear();
			product.getStep10_2Datas().add((Step10_2Data)obj);
		}else if(obj instanceof Step20Data){
			product.getStep20Datas().clear();
			product.getStep20Datas().add((Step20Data)obj);
		}else if(obj instanceof Step30_1Data){
			product.getStep30_1Datas().clear();
			product.getStep30_1Datas().add((Step30_1Data)obj);
		}else if(obj instanceof Step30_2Data){
			product.getStep30_2Datas().clear();
			product.getStep30_2Datas().add((Step30_2Data)obj);
		}else if(obj instanceof Step40Data){
			product.getStep40Datas().clear();
			product.getStep40Datas().add((Step40Data)obj);
		}else if(obj instanceof Step50Data){
			product.getStep50Datas().clear();
			product.getStep50Datas().add((Step50Data)obj);
		}else if(obj instanceof Step60Data){
			product.getStep60Datas().clear();
			product.getStep60Datas().add((Step60Data)obj);
		}else if(obj instanceof Step70Data){
			product.getStep70Datas().clear();
			product.getStep70Datas().add((Step70Data)obj);
		}else if(obj instanceof Step80Data){
			product.getStep80Datas().clear();
			product.getStep80Datas().add((Step80Data)obj);
		}else if(obj instanceof Step90Data){
			product.getStep90Datas().clear();
			product.getStep90Datas().add((Step90Data)obj);
		}else if(obj instanceof Step100_1Data){
			product.getStep100_1Datas().clear();
			product.getStep100_1Datas().add((Step100_1Data)obj);
		}else if(obj instanceof Step100_2Data){
			product.getStep100_2Datas().clear();
			product.getStep100_2Datas().add((Step100_2Data)obj);
		}else if(obj instanceof Step100_3Data){
			product.getStep100_3Datas().clear();
			product.getStep100_3Datas().add((Step100_3Data)obj);
		}else if(obj instanceof Step110Data){
			product.getStep110Datas().clear();
			product.getStep110Datas().add((Step110Data)obj);
		}else if(obj instanceof Step120_1Data){
			product.getStep120_1Datas().clear();
			product.getStep120_1Datas().add((Step120_1Data)obj);
		}else if(obj instanceof Step120_2Data){
			product.getStep120_2Datas().clear();
			product.getStep120_2Datas().add((Step120_2Data)obj);
		}else if(obj instanceof Step130Data){
			product.getStep130Datas().clear();
			product.getStep130Datas().add((Step130Data)obj);
		}else if(obj instanceof Step140_1Data){
			product.getStep140_1Datas().clear();
			product.getStep140_1Datas().add((Step140_1Data)obj);
		}else if(obj instanceof Step140_2Data){
			product.getStep140_2Datas().clear();
			product.getStep140_2Datas().add((Step140_2Data)obj);
		}else if(obj instanceof Step150Data){
			product.getStep150Datas().clear();
			product.getStep150Datas().add((Step150Data)obj);
		}else if(obj instanceof Step160Data){
			product.getStep160Datas().clear();
			product.getStep160Datas().add((Step160Data)obj);
		}else if(obj instanceof Step170_1Data){
			product.getStep170_1Datas().clear();
			product.getStep170_1Datas().add((Step170_1Data)obj);
		}else if(obj instanceof Step170_2Data){
			product.getStep170_2Datas().clear();
			product.getStep170_2Datas().add((Step170_2Data)obj);
		}else if(obj instanceof Step180Data){
			product.getStep180Datas().clear();
			product.getStep180Datas().add((Step180Data)obj);
		}else if(obj instanceof DataSheet){
			product.setDataSheet((DataSheet)obj);
		}
		
		String step = request.getParameter("step");
		if(step == null || step.length() == 0){
			Set<String> actives = this.stepService.findActiveActivityNames(product.getProcessId());
			if(actives != null && actives.size() > 0){
				for(String active : actives){
					request.setAttribute(active, active);
					log.debug("Active activity: " + active);
				}
			}
		}else{
			request.setAttribute(step, step);
		}
		request.setAttribute("product", product);
		List<Ituch> ituchs = new ArrayList<Ituch>();
		ituchs.add(new Ituch());
		ituchs.addAll(this.utilService.getAllItuchs());
		List<Itunm> itunms = new ArrayList<Itunm>();
		itunms.add(new Itunm());
		itunms.addAll(this.utilService.getAllItunms());
		request.setAttribute("ituchs", ituchs);
		request.setAttribute("itunms", itunms);
		String[] failedCells = this.stepService.findFailedCells(product.getProcessId());
		request.setAttribute("failedCells", failedCells);
		String page = "step/edit";
		return page;
	}
	
	private String goView(Model model, HttpServletRequest request, Long productId){
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
		Product product = this.stepService.getProduct(productId);
		request.setAttribute("product", product);
		model.addAttribute("View","View");
		String page = "step/edit";
		return page;
	}

}
